/**
 * 套餐管理初始化
 */
var VpnPackageInfo = {
    id: "VpnPackageInfoTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
VpnPackageInfo.initColumn = function () {
    return [[
            {title: '展示顺序', field: 'showSort', align: 'center',width:100},
            {title: '套餐名称', field: 'packageName',  align: 'center',width:180},
            {title: '原价格（USD）', field: 'price',align: 'center',width:180},
            {title: '原价格（HKD）', field: 'hkPrice',align: 'center',width:180},
            {title: '折扣', field: 'discount',align: 'center', width:100,templet:function (res) {
                    if (res.discount != null){
                        return res.discount * 10 +"折"
                    }
                }},
            {title: '折扣价格（USD）', field: 'discountPrice',align: 'center',width:180},
            {title: '折扣价格（HKD）', field: 'discountHkPrice',align: 'center',width:180},
            {title: '折扣提示', field: 'hintZh',align: 'center',width:200, templet:function (res) {
                    if (res.hintZh == null || res.hintZh == ''){
                        return "无"
                    }else{
                        return res.hintZh;
                    }
                }},
            {title: '折扣开始时间', field: 'startTime',align: 'center',width:200},
            {title: '折扣结束时间', field: 'endTime',align: 'center',width:200},
            {title: '折扣状态', field: 'discountStatus',align: 'center',width:100, templet:function (res) {
                    if(res.discountStatus == "0"){
                        return "已失效"
                    }
                    else if(res.discountStatus == '1'){
                        return "已生效"
                    }
                    else if(res.discountStatus == '2'){
                        return "未生效"
                    }else {
                        return "-"
                    }
                }},
            {title: '套餐状态', field: 'status', align: 'center',width:100, templet:function (res) {
                    if(res.status == "0"){
                        return "已下线"
                    }
                    else if(res.status == '1'){
                        return "已上线"
                    }
                    else if(res.status == '2'){
                        return "未激活"
                    }
                }},
            {title: '操作', field: 'status', align: 'center',width: 120, templet:function (res) {
                    var options =  "<div>";
                    if (res.status == 0){
                        options = options + "<a style='color: #06ffe6' onclick=\"checkPackageStatus(1,"+res.id+")\">上线</a>";
                    }else if (res.status == 1){
                        options = options + "<a style='color: #ff2822'onclick=\"checkPackageStatus(0,"+res.id+")\">下线</a>";
                        options = options +"<a style='color: #1E9FFF;margin-left: 20px;' onclick=\"openVpnPackageInfoUpdate("+res.id+")\">修改</a>" ;
                    }else if (res.status == 2){
                        options = options +"<a style='color: #1E9FFF;margin-left: 20px;' onclick=\"openVpnPackageInfoUpdate("+res.id+")\">修改</a>";
                    }
                    options = options  + "</div>";
                    return options;
                }}
    ]];
};

/**
 * 点击添加套餐管理
 */
VpnPackageInfo.openAddVpnPackageInfo = function () {
    var index = layer.open({
        type: 2,
        title: '添加套餐',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/vpnPackageInfo/vpnPackageInfo_add'
    });
    this.layerIndex = index;
};

VpnPackageInfo.openAddVpnPackageDiscountInfo = function () {
    var index = layer.open({
        type: 2,
        title: '新增套餐折扣',
        area: ['800px', '600px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/vpnPackageInfo/vpnPackageInfo_discount_add'
    });
    this.layerIndex = index;
};

/**
 * 打开修改套餐信息页面
 */
function openVpnPackageInfoUpdate(packageId) {
    var index = layer.open({
        type: 2,
        title: '修改套餐',
        area: ['800px', '600px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/vpnPackageInfo/vpnPackageInfo_update/' + packageId
    });
    this.layerIndex = index;
};

/**
 * 删除套餐管理
 */
VpnPackageInfo.delete = function () {
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/vpnPackageInfo/delete", function (data) {
            Feng.success("删除成功!");
            VpnPackageInfo.table.refresh();
        }, function (data) {
            Feng.error("删除失败!" + data.responseJSON.message + "!");
        });
        ajax.set("vpnPackageInfoId",this.seItem.id);
        ajax.start();
    }
};

function checkPackageStatus (status,id) {
    var hint = "你确定要停用这个套餐吗";
    if (status == 1){
        hint = "你确定要启用这个套餐吗"
    }

    layer.confirm(hint,{icon:3,title:'提示'},
        function (index) {
            var ajax = new $ax(Feng.ctxPath + "/vpnPackageInfo/updateStatus", function (data) {
                Feng.success("修改成功!");
                VpnPackageInfo.refresh();
            }, function (data) {
                Feng.error("修改失败!" + data.responseJSON.message + "!");
            });
            ajax.set("packageId",id);
            ajax.set("status",status);
            ajax.start();
            layer.close(index)
        },function (index) {
            layer.close(index)
        })
};

/**
 * 查询套餐管理列表
 */
VpnPackageInfo.search = function () {
    var queryData = {};
    queryData['condition'] = $("#condition").val();
    VpnPackageInfo.table.refresh({query: queryData});
};


VpnPackageInfo.tableList = function(queryData){
    layui.use("table",function () {
        var table = layui.table;

        VpnPackageInfo.table = table;

        table.render({
            elem: "#table",
            url: Feng.ctxPath +"/vpnPackageInfo/list",
            where: queryData,
            page: true,
            limit: 20,
            cols: VpnPackageInfo.initColumn(),
            parseData: function(res){
                return{
                    "code":0,
                    "msg":"获取成功",
                    "count":res.total,
                    "data": res.items
                }
            },
            text: {
                none: '暂无相关数据'
            },
            id: "tableReload"
        });
    });
};


/**
 * 刷新列表
 */
VpnPackageInfo.refresh = function(){
    VpnPackageInfo.table.reload("tableReload",{
        page: {
            curr: 1 //重新从第1页开始
        }
    });
};

$(function () {
    VpnPackageInfo.tableList();
});
